{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-24T01:08:25.397971Z",
     "start_time": "2025-11-24T01:08:24.400101Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "\n",
    "\"\"\"\n",
    "给定某股票连续10个交易日的收盘价Series:\n",
    "1.计算每日收益率(当日收盘价/前日收盘价-1)\n",
    "2.找出收益率最高和最低的日期\n",
    "3.计算波动率(收益率的标准差)\n",
    "\"\"\""
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n给定某股票连续10个交易日的收盘价Series:\\n1.计算每日收益率(当日收盘价/前日收盘价-1)\\n2.找出收益率最高和最低的日期\\n3.计算波动率(收益率的标准差)\\n'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T01:08:40.974177Z",
     "start_time": "2025-11-24T01:08:40.947115Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据准备\n",
    "prices = pd.Series([102.3, 103.5, 105.1, 104.8, 106.2, 107.0, 106.5, 108.1, 109.3, 110.2],\n",
    "                   index=pd.date_range('2023-01-01', periods=10))  # 起始时间， 时间段  （按顺序不重复产生）\n",
    "print(prices)"
   ],
   "id": "1ef28257b2f11983",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-01-01    102.3\n",
      "2023-01-02    103.5\n",
      "2023-01-03    105.1\n",
      "2023-01-04    104.8\n",
      "2023-01-05    106.2\n",
      "2023-01-06    107.0\n",
      "2023-01-07    106.5\n",
      "2023-01-08    108.1\n",
      "2023-01-09    109.3\n",
      "2023-01-10    110.2\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T01:23:00.870259Z",
     "start_time": "2025-11-24T01:23:00.855266Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.计算每日收益率(当日收盘价/前日收盘价-1)\n",
    "# 有固定方法\n",
    "p1 = prices.pct_change()\n",
    "print(p1)"
   ],
   "id": "477a43ccea8c4739",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-01-01         NaN\n",
      "2023-01-02    0.011730\n",
      "2023-01-03    0.015459\n",
      "2023-01-04   -0.002854\n",
      "2023-01-05    0.013359\n",
      "2023-01-06    0.007533\n",
      "2023-01-07   -0.004673\n",
      "2023-01-08    0.015023\n",
      "2023-01-09    0.011101\n",
      "2023-01-10    0.008234\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 2.找出收益率最高和最低的日期\n",
    "print(\"方法一：\")\n",
    "p2 = p1.sort_values(ascending=False).keys()\n",
    "print(p2)\n",
    "\n",
    "# 或\n",
    "print(\"方法二：\")\n",
    "print(p1.idxmax())\n",
    "print(p1.idxmin())"
   ],
   "id": "b5164fc6669cfadb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法一：\n",
      "DatetimeIndex(['2023-01-03', '2023-01-08', '2023-01-05', '2023-01-02',\n",
      "               '2023-01-09', '2023-01-10', '2023-01-06', '2023-01-04',\n",
      "               '2023-01-07', '2023-01-01'],\n",
      "              dtype='datetime64[ns]', freq=None)\n",
      "方法二：\n",
      "2023-01-03 00:00:00\n",
      "2023-01-07 00:00:00\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T01:26:33.817925Z",
     "start_time": "2025-11-24T01:26:33.810744Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3.计算波动率(收益率的标准差)\n",
    "p3 = p1.std()\n",
    "print(p3)"
   ],
   "id": "468862d2dc2fcc91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.007373623845361105\n"
     ]
    }
   ],
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
